package com.hbt.myplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbt.myplus.socket.ChatSocket;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.hbt.myplus.service.IPetOwnService;
import com.hbt.myplus.service.impl.PetOwnServiceImpl;
import com.hbt.myplus.model.PetOwn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hbt.myplus.utils.ResponseWeb;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
    * 普通用户（宠物主） 前端控制器
    * </p>
 *
 * @author astupidcoder
 * @since 2020-12-22
 * @version v1.0
 */
@RestController
@RequestMapping("pet/own")


public class PetOwnController {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private PetOwnServiceImpl petOwnService;

    /**
     * 查询分页数据
     */
    @PostMapping(value = "/login")
    public ResponseWeb all(@RequestHeader("openid")String openid, @RequestBody PetOwn petOwn){
        System.out.println("enter pet own login");
        petOwn.setOpenid(openid);
        //根据openid查询是否有数据
        PetOwn one = petOwnService.getOne(new QueryWrapper<PetOwn>().eq("openid", petOwn.getOpenid()));
        boolean flag = false;
        if (one != null){
            one.setAvatar(petOwn.getAvatar());
            one.setNickname(petOwn.getNickname());
            one.setOpenid(openid);
            flag = petOwnService.updateById(one);
        }else {
            flag = petOwnService.save(petOwn);
        }

        return new ResponseWeb(flag);
    }
    @GetMapping(value = "/online")
    public ResponseWeb getOnlineUsers(){
        List<ChatSocket> list = ChatSocket.socket_list.stream().filter(a -> !a.isDoc()).collect(Collectors.toList());
        if (list != null && !list.isEmpty()){
            List<String> openids = list.stream().map(a -> a.getOpenid()).collect(Collectors.toList());
            List<PetOwn> petOwnList = (List<PetOwn>)petOwnService.list(new QueryWrapper<PetOwn>().in("openid", openids));
            return new ResponseWeb(petOwnList);
        }
        return new ResponseWeb(new ArrayList<PetOwn>(0));
    }




    /**
     * 根据id查询
     */
    @GetMapping(value = "/{id}")
    public ResponseWeb<PetOwn> getById(@PathVariable("id") String id){
       return null;
    }

    /**
     * 新增
     */
    @PostMapping
    public ResponseWeb<PetOwn> add(@RequestBody PetOwn petOwn){
        return null;
    }

    /**
     * 删除
     */
    @DeleteMapping
    public ResponseWeb<String> delete(@RequestParam("ids") List<String> ids){
        return null;
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseWeb<PetOwn> update(@RequestBody PetOwn petOwn){
        return null;
     }

}
